package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 工单完成处理器
 */
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompletedMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;


    @Override
    public void process(String msg) throws IOException {

        //参数效验
        if (StringUtils.isBlank(msg) || msg == "") {

            throw new LogicException("工单协议参数错误");
        }

        //获取协议对象
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(msg, TaskCompleteContract.class);

        //获取工单类型
        int productTypeId = taskCompleteContract.getTaskType();
        //获取售货机编号
        String innerCode = taskCompleteContract.getInnerCode();
        //参数效验
        if (StringUtils.isBlank(innerCode) || innerCode == "") {

            throw new LogicException("工单协议参数错误");
        }
        //投放售货机工单类型完成,修改售货机状态为运营
        if (VMSystem.TASK_TYPE_DEPLOY == productTypeId) {
            vendingMachineService.updateStatus(innerCode, VMSystem.VM_STATUS_RUNNING);
        }


        //撤机售货机工单类型完成,修改售货机状态为撤机
        if (VMSystem.TASK_TYPE_REVOKE == productTypeId) {
            vendingMachineService.updateStatus(innerCode, VMSystem.VM_STATUS_REVOKE);
        }

    }
}
